#include <stdio.h>
#include <stdlib.h>
#include <math.h>


#define PI 3.14

double getDistancia(double latitude, double longitude, double latitudePto, double longitudePto){
        double dlon, dlat, a, distancia;
        dlon = longitudePto - longitude;
        dlat = latitudePto - latitude;
        a = pow(sin(dlat/2),2) + cos(latitude) * cos(latitudePto) * pow(sin(dlon/2),2);
        distancia = 2 * atan2(sqrt(a), sqrt(1-a));

        return 6371 * distancia; /* 6371 is the radius of the Earth in kms*/
}

double graustoRad(double graus){

	return ((graus * M_PI) / 180);
}

double minstograus(double min){

	return (min/60);
}
void main(){
	char latA,longA,latB,longB;
	double latGrausA,latMinsA,longGrausA,longMinsA, latGrausB,latMinsB,longGrausB,longMinsB;
	double latitude, longitude, latitudePto,longitudePto;


	printf("Insira 2 locais no seguinte formato: N/S latGrausAºminsA' E/W longGrausAºminsA' , N/S latGrausBºminB' E/W longGrausBºminB'\n");
	scanf("%c %lfº%lf' %c %lfº%lf' , %c %lfº%lf' %c %lfº%lf'", &latA,&latGrausA,&latMinsA,&longA,&longGrausA,&longMinsA, &latB,&latGrausB,&latMinsB,&longB,&longGrausB,&longMinsB);

	    //W or S são negativos!

	latitude = graustoRad(latGrausA) + graustoRad(minstograus(latMinsA));
	if (latA=='S') latitude*=-1;

	longitude = graustoRad(longGrausA) + graustoRad(minstograus(longMinsA));
	if (longA=='W') longitude*=-1;

	latitudePto = graustoRad(latGrausB) + graustoRad(minstograus(latMinsB));
	if (latB=='S') latitudePto*=-1;

	longitudePto = graustoRad(longGrausB) + graustoRad(minstograus(longMinsB));
	if (longB=='W') longitudePto*=-1;

	printf("Distância: %.2lf km\n",getDistancia(latitude, longitude, latitudePto, longitudePto));
}